package com.dao;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import com.base.BaseDao;
import com.bean.ParkManager;
import com.bean.Parkinfo;
import com.bean.Userinfo;
import com.tool.DateTimeTools;

public class ManagerAndUserinfoDao extends BaseDao {
	private static final Logger log = Logger.getLogger(ManagerAndUserinfoDao.class);
	/*
	 * 用户量统计
	 */
	public Double [][] queryByMonth(Map<Double, Double> summanager,String monthtime)
					throws Exception {
		session = this.getSession();
		
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
			//String datetime = "2014-05";
			Date timelong = formatDate.parse(monthtime);
			Timestamp inTime = new Timestamp(timelong.getTime());
			Timestamp outStartTime = new Timestamp(timelong.getTime());
			Timestamp outEndTime = new Timestamp(timelong.getTime());
			DateTimeTools.getMonthDays(inTime, outStartTime, outEndTime);//获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度

			int enddate = outEndTime.getDate();
			Double[][] data = new Double[2][enddate];
			for(int i=0; i<2; i++){
				for(int j=0; j<enddate; j++){
					if(i==0)
						data[i][j] = (double)(j+1);
					else
						data[i][j] = 0.0;
				}
			}
			
			String hql = "SELECT new com.bean.ParkManager(year(createTime),MONTH(createTime),day(createTime),count(*),status)" +
					" from ParkManager "+
					" where createTime like '"+monthtime+"%'"+
					" GROUP BY year(createTime),MONTH(createTime),day(createTime),status";
			Query query = session.createQuery(hql);
			List<ParkManager> list1 = query.list();
			if(list1!=null){
				for(ParkManager par : list1){
					if(par!=null){
						int x=GetIndexByStatus(par.getStatus());
						int y=par.getDay()-1;
						data[x][y] = (double) Math.abs(par.getSumcount());
					}
				}
			}else{
				return null;
			}
			log.info( data.toString());
			return data;
	}

	public Double[][] queryByMonths(Map<Double, Double> sumuserinfo,
			String monthtime) throws Exception {
		session = this.getSession();

		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
		// String datetime = "2014-05";
		Date timelong = formatDate.parse(monthtime);
		Timestamp inTime = new Timestamp(timelong.getTime());
		Timestamp outStartTime = new Timestamp(timelong.getTime());
		Timestamp outEndTime = new Timestamp(timelong.getTime());
		DateTimeTools.getMonthDays(inTime, outStartTime, outEndTime);// 获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度

		int enddate = outEndTime.getDate();
		Double[][] data = new Double[2][enddate];
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < enddate; j++) {
				if (i == 0)
					data[i][j] = (double) (j + 1);
				else
					data[i][j] = 0.0;
			}
		}

		String hql = "SELECT new com.bean.Userinfo( year(createTime),MONTH(createTime),day(createTime),count(*),status) "
				+ " from Userinfo "
				+ " where createTime like '"
				+ monthtime
				+ "%'"
				+ " GROUP BY year(createTime),MONTH(createTime),day(createTime),status";
		Query query = session.createQuery(hql);
		List<Userinfo> list = query.list();
		if (list != null) {
			for (Userinfo par : list) {
				if(par!=null){
					int x=GetIndexByStatus(par.getStatus());
					int y=par.getDay()-1;
					data[x][y] = (double) Math.abs(par.getSumcount());
				}
			}
		} else {
			return null;
		}

		log.info(data.toString());

		return data;
	}

	public Double [][] queryByYear(Map<Double, Double> summanager,String yeartime)
					throws Exception {

			int enddate = 12;// outEndTime.getDate();
			Double[][] data = new Double[2][enddate];
			for(int i=0; i<2; i++){
				for(int j=0; j<enddate; j++){
					if(i==0)
						data[i][j] = (double)(j+1);
					else
						data[i][j] = 0.0;
				}
			}

			session = this.getSession();
			String hql = "SELECT new com.bean.ParkManager(year(createTime),MONTH(createTime),count(*),status) "+
					" from ParkManager "+
					" where createTime like '"+yeartime+"%'"+
					" GROUP BY year(createTime),MONTH(createTime),status";
			Query query = session.createQuery(hql);
			List<ParkManager> list = query.list();
			if(list!=null){
				for(ParkManager par : list){
					if(par!=null){
						int x=GetIndexByStatus(par.getStatus());
						int y=par.getMonth()-1;
						data[x][y] = (double) Math.abs(par.getSumcount());
					}
				}
			}else{
				return null;
			}
			
			log.info(this.getClass()+".queryByYear()"+ data.toString());
			
			return data;
	}

	public Double[][] queryByYears(Map<Double, Double> sumuserinfo,
			String yeartime) throws Exception {

		int enddate = 12;// outEndTime.getDate();
		Double[][] data = new Double[2][enddate];
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < enddate; j++) {
				if (i == 0)
					data[i][j] = (double) (j + 1);
				else
					data[i][j] = 0.0;
			}
		}
		session = this.getSession();
		String hql = "SELECT new com.bean.Userinfo(year(createTime),MONTH(createTime),count(*),status) "
				+ " from Userinfo "
				+ " where createTime like '"
				+ yeartime
				+ "%'" + " GROUP BY year(createTime),MONTH(createTime),status";
		Query query = session.createQuery(hql);
		List<Userinfo> list1 = query.list();
		if (list1 != null) {
			for (Userinfo par : list1) {
				if(par!=null){
					
					int x=GetIndexByStatus(par.getStatus());
					int y=par.getMonth()-1;
					data[x][y] = (double) Math.abs(par.getSumcount());
				}
			}
		} else {
			return null;
		}

		log.info(this.getClass() + ".queryByYear()" + data.toString());

		return data;
	}
	public int GetIndexByStatus(int status){
		//-2：暂停营业中；-1：已删除；0：已冻结；1：未审核；2：审核中；3：正常
		switch(status){
		case -1:
			return 1;
		case 0:
			return 1;
		case 1:
			return 1;
		}
		return -100;
	}
}
